package org.tablaturehero;

import org.tablaturehero.services.IMusicService;
import org.tablaturehero.services.IMusicServiceListener;
import org.tablaturehero.services.MusicService;
import org.tablaturehero.services.MusicServiceBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class TestSound extends Activity implements SeekBar.OnSeekBarChangeListener, OnClickListener  {
	
	private TextView progressText;
	private SeekBar seekbar;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button playButton = (Button) findViewById(R.string.play);
        playButton.setOnClickListener(this);
 
        seekbar = (SeekBar) findViewById(R.string.seekBar);
        seekbar.setOnSeekBarChangeListener(this);
        progressText = (TextView)findViewById(R.string.progess);
    }

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//		progressText.setText( progress + " [getString(R.string.seekbar_from_touch)]=" + fromUser);
		progressText.setText((int)(100*speedModulationCalculation(progress)) + "%");
		//progress % of the music length
		Intent intent = new Intent(MusicService.ACTION_GOTO);
		intent.putExtra("PROGRESS", progress);
		startService(intent);
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		progressText.setText("seekbar_tracking_on");
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		progressText.setText("seekbar_tracking_off");
	}
	
	// retourne 50% du total à 0 de progression (1/2), et 200% à 100 de progression (x2)
	private double speedModulationCalculation(int progress) {
		
		double result;
		if(progress >= 50) {
			result = (double)progress/50.0;
		} else {
			result = 50.0/(100.0-(double)progress);
		}		
		return result;
	}

	@Override
	public void onClick(View v) {
	
		Button butt = (Button) v;
		
		switch(v.getId()) {
		
			case  R.string.play :
				
				Intent intent = new Intent(this, MusicService.class);
				intent.setAction(MusicService.ACTION_PLAY);
				startService(intent);
				
				//the play button become the pause button then !
				butt.setText(R.string.pause);
				butt.setId(R.string.pause);
				Button stopButton = (Button) findViewById(R.string.stop);
			    stopButton.setOnClickListener(this);		
			    
//			    MusicService.getService().addListener(new IMusicServiceListener() {
//					
//					@Override
//					public void musicProgressionChanged(Integer percentage) {
//						// put the seekbar in the right position 
//						seekbar.setProgress(percentage);
//					}
//				});
			    
			    //start service
			    ServiceConnection connection = new ServiceConnection() { 
			       			     
			        public void onServiceDisconnected(ComponentName name) { 
			            Log.i("MusicService", "Disconnected!"); 
			        }

					@Override
					public void onServiceConnected(ComponentName name, IBinder service) {
						  Log.i("MusicService", "Connected!"); 
				            IMusicService musicService = ((MusicServiceBinder)service).getService(); 
				            musicService.addListener(new IMusicServiceListener() {
								
								@Override
								public void musicProgressionChanged(Integer percentage) {
									// put the seekbar in the right position 
									seekbar.setProgress(percentage);
								}
							});
						
					}  
			    }; 
			        
			    bindService(intent,connection, Context.BIND_AUTO_CREATE); 
			    
				break;
				
			case R.string.pause :
				startService(new Intent(MusicService.ACTION_PAUSE));
				//the pause button become the play button again
				butt.setText(R.string.play);
				butt.setId(R.string.play);
				break;
				
			case R.string.stop :
				startService(new Intent(MusicService.ACTION_STOP));
				
				butt.setOnClickListener(null);
				//if the pause button is on, reset it to the play button !
				Button pauseButton = (Button) findViewById(R.string.pause);
				if(pauseButton!=null) {
					pauseButton.setText(R.string.play);
					pauseButton.setId(R.string.play);
				}
				break;		
		}
		
	}
	
}